package com.zgy.backend.bookstore.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zgy.backend.bookstore.mapper.BookMapper;
import com.zgy.backend.bookstore.pojo.Book;
import com.zgy.backend.bookstore.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: renjiaxin
 * @Despcription:
 * @Date: Created in 2020/7/21 2:33
 * @Modified by:
 */
@RestController
@RequestMapping("book")
public class BookController {

    @Autowired(required = false)
    private BookMapper mapper;

    @Autowired
    BookService service;


    /**
     * 获取总的数量书籍数量
     *
     * @return num
     */
    @GetMapping("num")
    public Integer getNum() {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("id", 0);
        return mapper.selectCount(queryWrapper);
    }


    /**
     * 获取所有的书籍
     *
     * @return list
     */
    @GetMapping("all")
    public List<Book> getAllBooks() {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("id", 0);
        return mapper.selectList(queryWrapper);
    }


    /**
     * 按照分页获取书籍
     *
     * @param page 第page页, 从0页开始
     * @param size 每页的数量
     * @return list
     */
    // https://www.cnblogs.com/shawWey/p/9842684.html rest风格
    @GetMapping("/get/{page}/{size}")
    public List<Book> getBookWithPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        // 参数一是当前页，参数二是每页个数
        IPage<Book> aPageBook = new Page<>(page, size);
        aPageBook = mapper.selectPage(aPageBook, null);
        List<Book> list = aPageBook.getRecords();
        return list;
    }

    /**
     * 增加book
     *
     * @param book
     * @return success or error
     */
    @PostMapping("savebook")
    public String saveBookEntity(@RequestBody Book book) {
        int res = service.saveBookEntity(book);
        if (res == 1) {
            return "success";
        } else {
            return "error";
        }
    }


    /**
     * 增加book
     *
     * @param name    name
     * @param author  author
     * @param publish publish
     * @param pages   pages
     * @param price   price
     * @return 受影响的数量
     */
    @PostMapping("savebookParam")
    public int saveBookEntity(String name, String author, String publish, Integer pages, Double price) {
        return service.saveBookEntity(name, author, publish, pages, price);
    }


    /**
     * 获取图书，rest风格
     *
     * @param id id
     * @return book
     */
    @GetMapping("getbook/{id}")
    public Book getBookById(@PathVariable("id") Integer id) {
        return service.getBookById(id);
    }


    /**
     * 获取图书，传统风格
     *
     * @param id id
     * @return book
     */
    @GetMapping("getbook")
    public Book getBookById02(Integer id) {
        return service.getBookById(id);
    }


    /**
     * 删除book
     *
     * @param id
     * @return 受影响数量
     */
    @DeleteMapping("/deletebook/{id}")
    public String deleteBookById(@PathVariable("id") Integer id) {
        Integer res = service.deleteBookById(id);
        if (res == 1) {
            return "success";
        } else {
            return "error";
        }
    }


    /**
     * 更新book，前端传过来的参数，默认是以json的形式传过来的，需要添加@RequestBody
     *
     * @param book
     * @return 受影响数量
     */
    @PostMapping("/updatebook")
    public String updateBookById(@RequestBody Book book) {
        int res = service.updateBookById(book);
        if (res == 1) {
            return "success";
        } else {
            return "error";
        }
    }
}
